package unit9_10;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/*
* 公共连接类
* */
public class JDBC_test2 {
    private static Connection conn;
    private static String address="jdbc:mysql://localhost:3306/school?" +
            "useSSL=true&characterEncoding=utf-8&user=root&password=123456";
    private static String dataName="root";
    private static String datapw="123456";

    public static Connection getConn(){
        if(conn==null){//如果为空没有就重新加载驱动
            try {
//                加载驱动
                Class.forName("com.mysql.cj.jdbc.Driver");
//                连接数据库
                conn= DriverManager.getConnection(address,dataName,datapw);
            } catch (Exception e) {
                e.printStackTrace();
//                加载失败
                System.out.println("连接失败！");
            }
        }
        return conn;
    }


}
